package com.cheer.controller;

import com.cheer.common.Result;
import com.cheer.dto.MallAddressAddDTO;
import com.cheer.dto.MallOrderAddDTO;
import com.cheer.dto.MallOrderRefundApplyDTO;
import com.cheer.service.AuthService;
import com.cheer.service.MallOrderService;
import com.cheer.utils.RequestUtil;
import com.cheer.vo.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/order")
public class MallOrderController {

    @Autowired
    private MallOrderService mallOrderService;

    @Autowired
    private AuthService authService;

    @ApiOperation("创建订单")
    @PostMapping("/add")
    public Result<MallOrderCreateVO> add(@RequestBody @Valid MallOrderAddDTO orderAddDTO,
                                         HttpServletRequest request, HttpSession session) {
        return Result.success(mallOrderService.add(orderAddDTO, authService.getUserId(session),
                authService.getOpenid(session), RequestUtil.getIpAddress(request)));
    }

    @ApiOperation("查询订单支付状态")
    @GetMapping("/status")
    public Result<Integer> status(@RequestParam Long id, HttpSession session) {
        return Result.success(mallOrderService.getPayStatus(id, authService.getUserId(session)));
    }

    @ApiOperation("查询订单列表")
    @GetMapping("/list")
    public Result<List<MallOrderFrontListVO>> list(@RequestParam(required = false) Integer status, HttpSession session) {
        return Result.success(mallOrderService.listByUserId(authService.getUserId(session), status));
    }

    @ApiOperation("查询退换售后订单列表")
    @GetMapping("/list/refund")
    public Result<List<MallOrderFrontListVO>> listRefund(HttpSession session) {
        return Result.success(mallOrderService.listRefundByUserId(authService.getUserId(session)));
    }

    @ApiOperation("查询物流")
    @GetMapping("/express")
    public Result<MallExpressInfoVO> getExpress(@RequestParam Long id,
                                                HttpSession session) {
        authService.getUserId(session);
        return Result.success(mallOrderService.getExpress(id));
    }

    @ApiOperation("查询订单详情")
    @GetMapping("/info")
    public Result<MallOrderFrontInfoVO> info(@RequestParam Long id, HttpSession session) {
        authService.getUserId(session);
        return Result.success(mallOrderService.frontInfo(id));
    }

    @ApiOperation("确认收货")
    @GetMapping("/finish")
    public Result finish(@RequestParam Long id, HttpSession session) {
        mallOrderService.finish(id, authService.getUserId(session));
        return Result.success();
    }

    @ApiOperation("取消订单")
    @GetMapping("/cancel")
    public Result cancel(@RequestParam Long id, HttpSession session) {
        mallOrderService.cancel(id, authService.getUserId(session));
        return Result.success();
    }

    @ApiOperation("付款")
    @PostMapping("/pay")
    public Result pay(@RequestParam Long id, HttpServletRequest request, HttpSession session) {
        return Result.success(mallOrderService.pay(id, authService.getUserId(session),
                authService.getOpenid(session), RequestUtil.getIpAddress(request)));
    }

    @ApiOperation("申请退款")
    @PostMapping("/refund")
    public Result refund(@RequestBody @Valid MallOrderRefundApplyDTO orderRefundApplyDTO, HttpSession session) {
        mallOrderService.refund(orderRefundApplyDTO, authService.getUserId(session));
        return Result.success();
    }

    @ApiOperation("查看退款详情")
    @GetMapping("/refund/info")
    public Result<MallRefundFrontInfoVO> refundInfo(@RequestParam Long id, HttpSession session) {
        return Result.success(mallOrderService.frontRefundInfo(id, authService.getUserId(session)));
    }

    @ApiOperation("查询所有订单简略信息")
    @GetMapping("/list/all")
    public Result<List<MallSimpleOrderVO>> listAll(HttpSession session) {
        return Result.success(mallOrderService.listSimplyByUserId(authService.getUserId(session)));
    }

    @ApiOperation("搜索订单")
    @GetMapping("/search")
    public Result<List<MallOrderFrontListVO>> search(String keyword, HttpSession session) {
        return Result.success(mallOrderService.search(authService.getUserId(session), keyword));
    }

    @ApiOperation("查看二维码")
    @GetMapping("/qrcode")
    public Result<MallQrCodeVO> getQrcode(@RequestParam Long id,
                                          HttpSession session) {
        authService.getUserId(session);
        return Result.success(mallOrderService.getQrcode(id));
    }

    @ApiOperation("抽奖")
    @GetMapping("/lottery")
    public Result<LotteryVO> startLottery(HttpSession session) {
        return Result.success(mallOrderService.startLottery(authService.getUserId(session)));
    }

    @ApiOperation("获取奖品")
    @GetMapping("/award")
    public Result<LotteryVO> award(HttpSession session) {
        return Result.success(mallOrderService.award(authService.getUserId(session)));
    }

    @GetMapping("/announcement")
    public Result<List<String>> getLotteryAnnouncement(HttpSession session) {
        authService.getUserId(session);
        return Result.success(mallOrderService.getLotteryAnnouncement());
    }
}
